'use strict';

require('../../assets/scss/entry/common.scss');
require('../../assets/scss/entry/agency.scss');

let $ = jQuery = window.jQuery = window.$ = require('jquery');
require('../../libs/jquery.cookie');

import * as api from '../../components/api/index.js';
import url from '../../components/util/url.js';
import util from '../../components/util/util.js';

let pageEntry = (function() {
	// let mediaId = url.getQuery('mediaId');
	/**
	 * 验证考生姓名
	 * @param exam {string} 考生姓名
	 */
	let verifyExam = (examName) => {
		//验证机考生姓名是否为空
		if (!examName) {
			// Layer.msg('考生姓名不能为空');
			$(".form-error").empty();
			$(".form-input input").css("borderColor","#e7e7e7");
			$(".examName").empty().html("*考生姓名不能为空");
			$("#examName").css("borderColor","rgb( 255, 141, 50 )");
			return;
		}
		return true;
	}
	/**
	 * 验证考生身份证
	 * @param examCode {Number} 考生身份证
	 */
	let verifyCode = (examCode) => {
		//验证身份证号是否为空
		if (!examCode) {
			$(".form-error").empty();
			$(".form-input input").css("borderColor","#e7e7e7");
			$(".examCode").empty().html("*身份证号不能为空");
			$("#examCode").css("borderColor","rgb( 255, 141, 50 )");
			return;
		}
		//验证身份证号位数
		if(examCode.length != 18 && examCode.length != 15){
			$(".form-error").empty();
			$(".form-input input").css("borderColor","#e7e7e7");
			$(".examCode").empty().html("*请输入正确的证件号");
			$("#examCode").css("borderColor","rgb( 255, 141, 50 )");
			return;
		}
		return true;
	}
	return {
		initCheck() {
			let nowUrl = window.location.href;
			util.initLogin(nowUrl,"2");
		},
		setWindowHeight() {
			// 获取视窗高度，将背景图铺满屏幕
			let wh = (window.innerHeight > 0) ? window.innerHeight : screen.height;
			$('body,.page-apply-form-indiviInfo').height(wh);
		},
		bindEvent() {
			// 绑定报名事件
			$(".link-submit").off("click").on("click", function(event) {
				let examName = $("#examName").val();//考生姓名
				let examCode = $("#examCode").val();//考生身份证
				let examType = $("#examType option:selected").val();//考试等级
				// let userCode = url.getQuery("userCode");
				let userCode = $.cookie("userCode");
				if(verifyExam(examName) && verifyCode(examCode)){
					$(".form-error").empty();
					$(".form-input input").css("borderColor","#e7e7e7");
					api.indiviEnrol(examName,userCode,examCode,examType).then(resp=>{
						if(resp.ret == "0"){
							// window.location = "/.html?registerType=1&agencyNumber="+resp.ret;
							$(".cover").show();
						}else{
							// if(resp.code == 400){//手机号不存在
							// 	$(".error").hide();
							// 	$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
							// 	$(".checkCode").empty().html("*"+resp.msg);
							// 	// $("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
							// 	$(".checkCode").show();
							// }else if(resp.code == 500){//验证码错误
							// 	$(".error").hide();
							// 	$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
							// 	$(".checkCode").empty().html("*"+resp.msg);
							// 	// $("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
							// 	$(".checkCode").show();
							// }else{//系统异常，请稍后再试
							// 	$(".error").hide();
							// 	$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
							// 	$(".checkCode").empty().html("*"+resp.msg);
							// 	// $("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
							// 	$(".checkCode").show();
							// }
							$(".error").hide();
							$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
							$(".examType").empty().html(resp.msg);
						}
						
					});
				}
			});
			//绑定关闭与确定事件
			$(".success-title i,.success-bottom").off("click").on("click",function(event){
				// $(".cover").hide();
				window.location.href = "/indiviInfo.html";
			})
		}
	}
})();

pageEntry.initCheck();
pageEntry.setWindowHeight();
pageEntry.bindEvent();
